<template>
  <div class="sidebar">
    <div class="sidebar-logo">
      <img src="@/assets/logo.png" alt="灵溪">
    </div>
    <div class="sidebar-logo2">
      <img src="@/assets/logo2.png" alt="灵溪">
    </div>
    <div class="menu">
      <el-menu
          style="margin-left: 15px"
        :default-active="activeMenu"
        class="el-menu-vertical"
        :collapse="isCollapse"
        background-color="transparent"
        text-color="#302e34"
        router
        active-text-color="#7E4CE0">

        <el-menu-item index="/app">
          <i class="el-icon-plus"></i>
          <span>新建会话</span>
        </el-menu-item>

        <el-menu-item index="/admin" v-if="userInfo.role_id === 1">
          <i class="el-icon-user"></i>
          <span>管理终端</span>
        </el-menu-item>

        <el-menu-item index="/setting">
          <i class="el-icon-setting"></i>
          <span>系统功能</span>
        </el-menu-item>
      </el-menu>

      <div class="divider"></div>
      <div class="history-title">历史记录</div>
      <div class="history-list">
        <div :class="['history-item', activeName === 'text' ? 'active' : '' ]" @click="toHistory('text')">
          <div class="item-left">
            <i class="el-icon-document"></i>
            <span>文本</span>
          </div>
          <div class="count" v-if="textCount > 0">{{ textCount }}</div>
        </div>
        <div :class="['history-item', activeName === 'image' ? 'active' : '' ]" @click="toHistory('image')">
          <div class="item-left">
            <i class="el-icon-picture"></i>
            <span>图像</span>
          </div>
          <div class="count" v-if="imageCount > 0">{{ imageCount }}</div>
        </div>
      </div>
    </div>

    <div class="sidebar-footer">
      <div class="user-info">
        <el-dropdown trigger="click" @command="handleCommand">
          <div class="user-profile">
            <el-avatar :size="32" :src="userInfo.avatar_url"></el-avatar>
            <div class="user-details">
              <span class="username">{{ userInfo.nickname }}</span>
              <span class="phone">{{ userInfo.phone }}</span>
            </div>
          </div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="profile">个人信息</el-dropdown-item>
            <el-dropdown-item command="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>

      <div class="theme-toggle" :class="{ 'dark': darkMode }"  @click="toggleDarkMode">
        <div class="switch-light" ></div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { http } from '@/api'

export default {
  name: 'Sidebar',
  data() {
    return {
      isCollapse: false,
      textCount: 0,
      imageCount: 0
    }
  },
  computed: {
    ...mapState('user', ['userInfo']),
    ...mapState('theme', ['darkMode']),
    activeMenu() {
      const path = this.$route.path;
      // 如果是对话页面，返回父路径
      if (path.startsWith('/conversation/')) {
        return '/app';
      }
      return path;
    },
    activeName() {
      if (this.$route.name === 'history') {
        return this.$route.query.type
      }
      return  ''
    }
  },
  created() {
    this.getConversationCounts()
  },
  methods: {
    toggleDarkMode() {
      this.$store.dispatch('theme/toggleDarkMode')
    },
    toHistory(type) {
      this.$router.push({
        path: '/setting/history',
        query: {
          type,
          time: new Date().getTime()
        }
      })
    },
    async getConversationCounts() {
      if (!this.userInfo?.id) return
      try {
        const res = await http.conversation.list({
          user_id: this.userInfo.id
        })
        // 统计文本和图像会话数量
        this.textCount = res.data.filter(item => item.type === 'text').length
        this.imageCount = res.data.filter(item => item.type === 'image').length
      } catch (error) {
        console.error('获取会话统计失败:', error)
      }
    },
    handleCommand(command) {
      switch (command) {
        case 'profile':
          this.$router.push('/setting/userInfo')
          break
        case 'logout':
          this.$store.dispatch('user/logout')
          this.$router.push('/login')
          break
      }
    }
  }
}
</script>

<style lang="scss">
.sidebar {
  height: 100%;
  width: 280px;
  background: #fff;
  border-right: 1px solid #eee;
  display: flex;
  flex-direction: column;
  border-radius: 16px;
  .sidebar-logo {
    height: 60px;
    display: flex;
    align-items: center;
    padding: 0 20px;
    margin-top: 5px;
    margin-bottom: -5px;
    img {
      height: 55px;
    }
  }
  .sidebar-logo2 {
    display: none;
  }
  .menu {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;

    .divider {
      height: 1px;
      background: rgba(0, 0, 0, 0.06);
      margin: 16px;
    }

    .history-title {
      font-size: 12px;
      color: rgba(0, 0, 0, 0.45);
      padding: 0 24px;
      margin-bottom: 8px;
    }

    .history-list {
      padding: 0 16px;

      .history-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: 40px;
        padding: 0 12px;
        margin: 4px 0;
        border-radius: 8px;
        cursor: pointer;
        transition: all 0.3s;
        &.active {
          background: #7E4CE0
        }
        &:hover {
          background: #7E4CE0
        }

        .item-left {
          display: flex;
          align-items: center;
          gap: 12px;

          i {
            font-size: 16px;
            color: #666;
          }

          span {
            font-size: 14px;
            color: #302e34;
          }
        }

        .count {
          min-width: 16px;
          height: 16px;
          padding: 0 4px;
          background: #7E4CE0;
          color: white;
          border-radius: 8px;
          font-size: 12px;
          line-height: 16px;
          text-align: center;
        }
      }
    }

    .el-menu {
      border: none;
      width: 240px;
    }

    .el-menu-item {
      position: relative;
      height: 40px;
      line-height: 40px;
      margin: 8px 0;
      border-radius: 12px;
      padding: 0 8px !important;

      &:hover {
        background: #7E4CE0 !important;
        color: #fff !important;
        i {
          color: #fff !important;
        }
      }

      &.is-active {
        background: #7E4CE0 !important;
        color: #fff !important;
        font-weight: 500;

        &::after {
          display: none;
        }
        i {
          color: #fff !important;
        }
      }

      i {
        margin-right: 12px;
        font-size: 18px;
      }

      .count {
        position: absolute;
        right: 16px;
        top: 50%;
        transform: translateY(-50%);
        min-width: 16px;
        height: 16px;
        padding: 0 4px;
        background: #7E4CE0;
        color: white;
        border-radius: 8px;
        font-size: 12px;
        line-height: 16px;
        text-align: center;
      }
    }
  }

  .sidebar-footer {
    padding: 16px;
    display: flex;
    flex-direction: column;
    width: 100%;
    .user-info {
      width: 100%;
      border-radius: 12px;
      box-shadow: 0 0 8px 0 rgba(232, 237, 250, .6), 0 2px 4px 0 rgba(232, 237, 250, .5);
      .user-profile {
        display: flex;
        align-items: center;
        cursor: pointer;
        padding: 12px 16px;
        transition: all 0.3s ease;

        &:hover {
          background: rgba(126, 76, 224, 0.04);
        }

        .user-details {
          margin-left: 14px;
          display: flex;
          flex-direction: column;

          .username {
            font-size: 14px;
            font-weight: 500;
            color: #1C1C28;
            margin-bottom: 2px;
          }

          .phone {
            font-size: 12px;
            color: #666;
          }
        }
      }
    }

    .theme-toggle {
      height: 36px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-radius: 12px;
      transition: all 0.3s ease;
      background: rgba(126, 76, 224, 0.04);
      position: relative;
      margin-top: 12px;
      margin-bottom: 10px;
      cursor: pointer;
      .switch-light {
        position: absolute;
        right: 0;
        top: 0;
        width: 50%;
        height: 100%;
        border-radius: 12px;
        background-color: #fff;
        background-image: url("~@/assets/images/light-switch.png");
        background-repeat: no-repeat;
        background-size: 100% 100%;
        transition: all 0.3s ease;
      }
    }
  }
}
.dark-mode {
  .chat-header-logo h1,
  .message-item .name {
    color: #7E4CE0 !important
  }
  .sidebar {
    .sidebar-logo2 {
      height: 60px;
      display: flex;
      align-items: center;
      padding: 0 20px;
      margin-top: 5px;
      margin-bottom: -5px;
      img {
        height: 65px;
      }
    }
    .sidebar-logo {
      display: none;
    }
    i {
      color: #ffffff;
    }
    .el-menu-item span{
      color: #ffffff;
    }
    .history-item .item-left{
      i {
        color: #ffffff !important;
      }
      span {
        color: #ffffff !important;
      }
    }
    .theme-toggle .switch-light {
      left: 0;
      right: initial;
    }
  }

}
.el-menu-vertical:not(.el-menu--collapse) {
  width: 240px;
}
</style>
